/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.tests.persistance;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import ar.com.wholeconsulting.pacc.model.Fluido;

/**
 * 
 * @author pable
 */
public class FluidoParcer {

	public static Fluido parseLine(String line) {
		Fluido fluido = new Fluido();
		line = line.replaceAll(",", ".");
		line = line.replaceAll("\"", "");
		String[] vec = line.split(";");
		fluido.setNombre(vec[1]);
		fluido.setMasaMolecular(Double.parseDouble(vec[2]));
		fluido.setSgg(Double.parseDouble(vec[3]));
		fluido.setCoeficienteIncompresibilidad(Double.parseDouble(vec[4]));
		fluido.setK(Double.parseDouble(vec[5]));
		fluido.setPuntoDeInflamacion(Integer.parseInt(vec[6]));
		fluido.setPuntoDeEbullicion(Integer.parseInt(vec[7]));
		fluido.setDensidad(Double.parseDouble(vec[8]));
		fluido.setSgl(Double.parseDouble(vec[9]));
		fluido.setCalorLatenteDeVaporizacion(Integer.parseInt(vec[10]));
		return fluido;
	}

	public static void main(String[] args) throws Exception {
		// JPAUtil util = new JPAUtil();
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("PACC");
		EntityManager manager = factory.createEntityManager();
		EntityTransaction transaction = manager.getTransaction();
		transaction.begin();

		Query query = manager.createNamedQuery(Fluido.FIND_ALL);
		List<Fluido> fluidos = query.getResultList();
		for (Fluido a : fluidos) {
			System.out.println(a.getNombre());
			System.out.println("----------------------------------------");
		}

//		try {
//			// Open the file that is the first
//			// command line parameter
//			FileInputStream fstream = new FileInputStream("C:/Users/Duco Pablo/Documents/NetBeansProjects/PACC/src/main/resources/base/Fluidos.csv");
//			// Get the object of DataInputStream
//			DataInputStream in = new DataInputStream(fstream);
//			BufferedReader br = new BufferedReader(new InputStreamReader(in));
//			String strLine;
//			// Read File Line By Line
//			while ((strLine = br.readLine()) != null) {
//				// Print the content on the console
//				manager.persist(parseLine(strLine));
//			}
//			// Close the input stream
//			in.close();
//		} catch (Exception e) {// Catch exception if any
//			System.err.println("Error: " + e.getMessage());
//		}
//
//		transaction.commit();

		manager.close();
		factory.close();
	}
}
